<?php /* УТФ-8:fix */

class SPImodel__front extends SPImodel__dummy
{
    public function select_global_search( $qstring, $limit, $offset = 0 )
    {
        $qstring = trim( preg_replace( '/[ %_]+/', ' ', $qstring ) );
        $words = explode( ' ', $qstring );

        $query_words = '';

        foreach( $words as $word )
        {
            $query_words .= ( $query_words ? " AND " : '' )
                          . " %FIELD% ILIKE '%" . addslashes( $word ) . "%' "
                          ;
        }

        //------------------------------------------------------------------------

        $limit  = (int) $limit;
        $offset = (int) $offset;

        $query = "      (
                            SELECT CASE WHEN type='recept'        THEN 10
                                        WHEN type='blog_recept'   THEN 10
                                        WHEN type='diet'          THEN 15
                                        WHEN type='alcohol'       THEN 20
                                        WHEN type='cocktail'      THEN 23
                                        WHEN type='snack'         THEN 25
                                        WHEN type='decor_table'   THEN 50
                                        WHEN type='decor_napkin'  THEN 50
                                        WHEN type='decor_dish'    THEN 50
                                        WHEN type='decor_kitchen' THEN 50
                                        WHEN type='new_cookware'  THEN 90
                                        WHEN type='menutvprogram' THEN 60
                                        WHEN type='news'          THEN 80
                                        WHEN type='article'       THEN 80
                                        WHEN type='blog_article'  THEN 80
                                        WHEN type='cookware'      THEN 91
                                        WHEN type='technics'      THEN 91
                                        WHEN type='phrasebook'    THEN 100
                                        WHEN type='unproduct'     THEN 110
                                        ELSE                           999 END AS priorety
                                 , id
                                 , pid_profile_addby
                                 , 'content' AS topic
                                 , type
                                 , pid_catalog AS pid
                                 , COALESCE( pubdate, t_created ) AS timestamp
                                 , name
                                 , anons
                                 , CASE WHEN type='recept'   THEN howtocook
                                        WHEN type='cocktail' THEN howtocook
                                        WHEN type='snack'    THEN howtocook
                                                             ELSE description END AS description
                              FROM ".SPIcfg::$store['table:content']."
                             WHERE workstatus=1
                               AND ".str_replace('%FIELD%',' (name || anons || description || howtocook || givein ) ',$query_words)."
                          ORDER BY priorety ASC, timestamp DESC
                             /*LIMIT {$limit}*/
                        )

                        UNION ALL

                        (
                            SELECT 30, id, pid_profile_addby, 'books', NULL, NULL, t_created AS timestamp, name, anons, description
                              FROM ".SPIcfg::$store['table:books']."
                             WHERE workstatus=1
                               AND ".str_replace('%FIELD%',' (name || anons || description) ',$query_words)."
                          ORDER BY timestamp DESC
                             /*LIMIT {$limit}*/
                        )

                        UNION ALL

                        (
                            SELECT 60, id, pid_profile_addby, 'etiquette', NULL, pid_etiquette_catalog, pubdate AS timestamp, name, anons, description
                              FROM ".SPIcfg::$store['table:etiquette_items']."
                             WHERE workstatus=1
                               AND ".str_replace('%FIELD%',' (name || anons || description) ',$query_words)."
                          ORDER BY timestamp DESC
                             /*LIMIT {$limit}*/
                        )

                        UNION ALL

                        (
                            SELECT 150, id, pid_profile_addby, 'singlepage', tid, NULL, t_created AS timestamp, name, NULL, description
                              FROM ".SPIcfg::$store['table:singlepages']."
                             WHERE ".str_replace('%FIELD%',' (name || description) ',$query_words)."
                          ORDER BY timestamp DESC
                             /*LIMIT {$limit}*/
                        )
                  ORDER BY priorety ASC, timestamp DESC
                     LIMIT {$limit} OFFSET {$offset}
                    ";

        return SPIstore::I()->query( $query );
    }

    public function select_rss_content( $select_mode = 'all', $limit = 25 )
    {
        $queries = array();
        $limit = (int) $limit;

        if( in_array( $select_mode, array( 'all', 'news', 'recept', 'diet', 'barmenu', 'decor', 'cookware' ) ) === true )
        {
            $types = array();
            if( $select_mode == 'all' || $select_mode == 'news' )     $types = array_merge( $types, array( 'news', 'article' ) );
            if( $select_mode == 'all' || $select_mode == 'recept' )   $types = array_merge( $types, array( 'recept' ) );
            if( $select_mode == 'all' || $select_mode == 'diet' )     $types = array_merge( $types, array( 'diet' ) );
            if( $select_mode == 'all' || $select_mode == 'barmenu' )  $types = array_merge( $types, array( 'alcohol', 'cocktail', 'snack' ) );
            if( $select_mode == 'all' || $select_mode == 'decor' )    $types = array_merge( $types, array( 'decor_table', 'decor_napkin', 'decor_dish', 'decor_kitchen' ) );
            if( $select_mode == 'all' || $select_mode == 'cookware' ) $types = array_merge( $types, array( 'new_cookware', 'cookware', 'technics' ) );

            $queries[] =    " SELECT id "
                       .         " , 'content' AS topic "
                       .         " , type "
                       .         " , pid_catalog AS pid "
                       .         " , COALESCE( pubdate, t_created ) AS timestamp "
                       .         " , name "
                       .         " , anons "
                       .         " , CASE WHEN type='recept'   THEN howtocook "
                       .         "        WHEN type='cocktail' THEN howtocook "
                       .         "        WHEN type='snack'    THEN howtocook "
                       .         "                             ELSE description END AS description "
                       .      " FROM ".SPIcfg::$store['table:content']
                       .     " WHERE workstatus=1 "
                       .       " AND type IN( '".join( "','", $types )."' ) "
                       .  " ORDER BY timestamp DESC "
                       .     " LIMIT {$limit} "
                       ;

            unset( $types );
        }

        if( in_array( $select_mode, array( 'all', 'recept' ) ) === true )
        {
            $queries[] =    " SELECT id "
                       .         " , 'books' AS topic "
                       .         " , NULL AS type "
                       .         " , NULL AS pid "
                       .         " , t_created AS timestamp "
                       .         " , name "
                       .         " , anons "
                       .         " , description "
                       .      " FROM ".SPIcfg::$store['table:books']
                       .     " WHERE workstatus=1 "
                       .  " ORDER BY timestamp DESC "
                       .     " LIMIT {$limit} "
                       ;
        }

        if( in_array( $select_mode, array( 'all', 'etiquette' ) ) === true )
        {
            $queries[] =    " SELECT id "
                       .         " , 'etiquette' AS topic "
                       .         " , NULL AS type "
                       .         " , pid_etiquette_catalog AS pid "
                       .         " , pubdate AS timestamp "
                       .         " , name "
                       .         " , anons "
                       .         " , description "
                       .      " FROM ".SPIcfg::$store['table:etiquette_items']
                       .     " WHERE workstatus=1 "
                       .  " ORDER BY timestamp DESC "
                       .     " LIMIT {$limit} "
                       ;
        }

        if( count( $queries ) == 0 )
        {
            return false;
        }
        elseif( count( $queries ) == 1 )
        {
            $query = $queries[0];
        }
        else
        {
            $query = " ( " . join( " ) UNION ALL ( ", $queries ) . " ) "
                   . " ORDER BY timestamp DESC "
                   . " LIMIT {$limit} "
                   ;
        }

        return SPIstore::I()->query( $query );
    }
}
